package com.cqu.hhx.driver.controller;

import com.cqu.hhx.driver.entity.*;
import com.cqu.hhx.driver.entity.forWeb.Evaluation;
import com.cqu.hhx.driver.service.ManageService;
import com.cqu.hhx.driver.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DJ
 * @create 2020-05-24 22:17
 * 订单控制类
 */
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 乘客创建新的订单
     * 乘客只能创建一个新的订单
     * 传入参数：乘客Id,价格,起点,目的地,距离
     * @param newOrder
     * @return 0：下单成功，1：下单失败,存在未完成订单 2:下单失败，未知原因 3:账户余额不够
     */
    @RequestMapping(value = "/customer/create/new/order",method = RequestMethod.POST)
    public Integer customerCreateNewOrder(@RequestBody NewOrder newOrder){
        newOrder.setCreateTime(new Timestamp(System.currentTimeMillis()));
        Integer status=orderService.addNewOrder(newOrder);
        return status;
    }

    /**
     * 司机请求新订单
     * @return 成功集合，失败null
     */
    @RequestMapping(value = "/driver/request/new/order",method = RequestMethod.GET)
    public List<NewOrder> driverRequestNewOrder(){
        List<NewOrder> orderList=orderService.getAllNewOrder();
        if(orderList!=null){
            for(NewOrder order:orderList){
                Timestamp usedTime=order.getCreateTime();
                order.setUsedTime(usedTime.toString());
            }
        }
        return orderList;
    }

    /**
     * 乘客获取新下订单
     * @param customerId
     * @return
     */
    @RequestMapping(value = "/customer/{customerId}/get/new/order",method = RequestMethod.GET)
    public NewOrder customerGetNewOrder(@PathVariable Long customerId){
        NewOrder newOrder=orderService.customerGetNewOrder(customerId);
        return newOrder;
    }

    /**
     * 乘客取消订单
     * @param newOrder
     * @return 0:成功 1：失败
     */
    @RequestMapping(value = "/customer/delete/new/order",method = RequestMethod.POST)
    public Map<String,Object> customerDeleteNewOrder(@RequestBody NewOrder newOrder){
        Map<String,Object> map=new HashMap<>();
        Integer status = orderService.deleteNewOrderByOrderId(newOrder.getOrderId());
        map.put("status",status);
        return map;
    }

    /**
     * 司机接单
     * @param driverId
     * @param orderId
     * @return 0：接单成功 1：接单失败 2:接单失败，已有进行中订单
     * Q1:并未处理同步问题
     */
    @RequestMapping(value = "/driver/{driverId}/accept/order/{orderId}",method = RequestMethod.GET)
    public Integer driverAcceptOrder(@PathVariable Long driverId,
                                     @PathVariable Long orderId){
        Integer status=1;
        DoingOrder acceptOrder=orderService.driverGetDoingOrder(driverId);
        if(acceptOrder!=null){
            return 2;
        }
        NewOrder newOrder=orderService.getNewOrderByOrderId(orderId);
        if (newOrder != null) {
            Timestamp getTime=new Timestamp(System.currentTimeMillis());
            Driver driver=orderService.getDriverInfoById(driverId);
            DoingOrder doingOrder=new DoingOrder(newOrder,getTime,driverId,driver.getDriverName());
            int Ok1=orderService.deleteNewOrderByOrderId(orderId);
            if(Ok1==0){
                //写入doing订单
                int OK2=orderService.addDoingOrder(doingOrder);
                if(OK2==0) return 0;
                else return 1;
            }else{
                return 1;
            }
        }
        return status;
    }

    /**
     * 前提：乘客成功创建订单之后
     * 乘客获取是否接单信息表
     * @param customerId
     * @return
     */
    @RequestMapping(value = "/customer/{customerId}/get/doing/order",method = RequestMethod.GET)
    public DoingOrder customerGetDoingOrder(@PathVariable Long customerId){
        DoingOrder order=orderService.customerGetDoingOrder(customerId);
        if(order!=null){
            order.setUsedTime(order.getGetTime().toString());
        }
        return order;
    }

    /**
     * 前提：司机成功接单后
     * 司机获取进行中订单信息
     * @param driverId
     * @return
     */
    @RequestMapping(value = "/driver/{driverId}/get/doing/order",method = RequestMethod.GET)
    public DoingOrder driverGetDoingOrder(@PathVariable Long driverId){
        DoingOrder order=orderService.driverGetDoingOrder(driverId);
        if(order!=null){
            order.setUsedTime(order.getGetTime().toString());
        }
        return order;
    }

    /**
     * 乘客确认订单完成
     * @param customerId
     * @return
     * 状态码：0:确认成功 1:确认失败，未知原因 2:确认失败，当前订单不存在
     * 订单Id：orderId
     */
    @RequestMapping(value = "/customer/{customerId}/check/finish/order",method = RequestMethod.GET)
    public Map<String,Object> customerCheckFinishOrder(@PathVariable Long customerId){
        Map<String,Object> map = new HashMap<>();
        //1.查询订单信息
        DoingOrder order=orderService.customerGetDoingOrder(customerId);
        if(order!=null){
            //2.删除订单信息
            int ok=orderService.deleteDoingOrderById(order.getOrderId());
            if(ok==0){
                Timestamp finishTime=new Timestamp(System.currentTimeMillis());
                FinishOrder finishOrder=new FinishOrder(order,finishTime);
                orderService.addFinishOrder(finishOrder);
                map.put("orderId",finishOrder.getOrderId());
                //更新乘客账户余额
                Float money1=orderService.updateCustomerMoney(customerId,order.getPrice(),false);
                //更新司机账户余额
                if(money1>0){
                    money1=orderService.updateDriverMoney(order.getDriverId(),order.getPrice(),true);
                    if(money1<0){
                        map.put("status",1);
                        return map;
                    }
                }else{
                    map.put("status",1);
                    return map;
                }
                map.put("status",0);
                return map;
            }else{
                map.put("status",1);
                return map;
            }
        }else{
            map.put("status",2);
            return map;
        }
    }

    /**
     * 乘客评价订单
     * @param evaluation
     * @return 0:代表完成 1:代表失败
     */
    @RequestMapping(value = "/customer/evaluate/order",method = RequestMethod.POST)
    public Integer customerEvaluateOrder(@RequestBody Evaluation evaluation){
            if(evaluation!=null){
                Integer status=orderService.evaluateOrder(evaluation);
                return status;
            }else{
                return 1;
            }

    }

    /**
     * 乘客获取已完成订单列表
     * @param customerId
     * @return
     */
    @RequestMapping(value = "/customer/{customerId}/get/finish/order",method = RequestMethod.GET)
    public List<FinishOrder> customerGetFinishOrder(@PathVariable Long customerId){
        List<FinishOrder> orders=orderService.customerGetFinishOrders(customerId);
        return orders;
    }

    /**
     * 司机获取已完成订单列表
     * @param driverId
     * @return
     */
    @RequestMapping(value = "/driver/{driverId}/get/finish/order",method = RequestMethod.GET)
    public List<FinishOrder> driverGetFinishOrder(@PathVariable Long driverId){
        List<FinishOrder> orders=orderService.driverGetFinishOrders(driverId);
        if(orders!=null){
            for(FinishOrder order:orders){
                order.setUsedTime(order.getFinishTime().toString());
            }
        }
        return orders;
    }

    /**
     * 管理员删除已完成订单
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/manager/delete/finish/order/{orderId}",method = RequestMethod.GET)
    public Integer managerDeleteFinishOrder(@PathVariable Long orderId){
        Integer status=orderService.deleteFinishOrder(orderId);
        return status;
    }

}
